<?php

namespace app\common\service;

use app\common\enum\point\order\PayStatusEnum;
use app\common\enum\transaction\SourceTypeEnum;
use app\common\enum\transaction\TypeEnum;
use app\common\model\Transaction;
use app\user\model\UserMoney;
use app\http\exception\ApiException;
use Exception;
use randy\tools\units\ExtendQueryHelper;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Db;

class UserMoneyService extends BaseService
{
    public function getUserMoneyList(ExtendQueryHelper $extendQueryHelper, $data)
    {
        $query = UserMoney::build()
                          ->where($extendQueryHelper->where)
                          ->order($extendQueryHelper->sort, $extendQueryHelper->order);
        $list = $extendQueryHelper->is_page ? $query->_paginate() : $query->select();
        return $list;
    }

    public function getUserMoneyItem($id)
    {
        return UserMoney::build()
            ->findOrFail($id);
    }

    public function delUserMoneyItem($id)
    {
        $info = UserMoney::find($id);
        return $info->delete();
    }

    public function setUserMoneyItem($id, $data)
    {
        $info = UserMoney::find($id);
        $info->save($data);
        return $info;
    }


    public function addUserMoneyItem($data)
    {
        $model = new UserMoney();
        $model->save($data);
        return $model;
    }

    /**
     * 积分账户调整
     * @param int            $point
     * @param int            $user_id
     * @param TypeEnum       $type
     * @param string         $source_no
     * @param SourceTypeEnum $source_type
     * @param string         $remark
     * @param string         $cause
     *
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @author : Randy_chen
     * @Date   : 2023/5/19
     * @Time   : 7:06
     */
    public static function changeUserPoint($point, $user_id, $type, $source_no, $source_type, $remark, $cause ,$article_id=0)
    {
        if ($point == 0) {
            return '';
        }
        if (
            in_array($source_type, [
                SourceTypeEnum::FB->value,
                SourceTypeEnum::PL->value,
                SourceTypeEnum::FX->value,
                SourceTypeEnum::GKSP->value
            ])) {
            if (
                Transaction::build()
                           ->where('user_id', $user_id)
                           ->where('source_type', $source_type)
                           ->where('create_date', date('Y-m-d'))
                           ->find()) {
                return '';
            }
        }

        Db::startTrans();
        // 积分账户调整
        try {
            $userMoney = UserMoney::build()
                                  ->lock()
                                  ->where('user_id', $user_id)
                                  ->find();
            if (!$userMoney) {
                error_abort('当前用户账户不存在,请联系管理员!');
            }
            $qc_balance = $userMoney->points;
            $userMoney->total_points = Db::raw('total_points +' . $point);
            $userMoney->points = Db::raw('points +' . $point);
            $userMoney->save();
            $transaction = new Transaction();
            $transaction->user_id = $user_id;
            $transaction->action = get_client_name();
            $transaction->type = $type;
            $transaction->article_id = $article_id;
            $transaction->qc_balance = $qc_balance;
            $transaction->amount = $point;
            $transaction->balance = $qc_balance+$point;
            $transaction->source_no = $source_no;
            $transaction->source_type = $source_type;
            $transaction->remark = $remark;
            $transaction->create_date = date('Y-m-d');
            $transaction->cause = $cause;
            $transaction->module = 'points';
            $transaction->save();
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            throw $e;
        }
    }
}
